package com.bookstore.back.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.bookstore.back.entity.UserAddress;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 用户地址Mapper接口
 * 
 * @author 程序猿_Ti
 * @since 2025-07-22
 */
@Mapper
public interface UserAddressMapper extends BaseMapper<UserAddress> {

    /**
     * 根据用户ID查询地址列表
     */
    @Select("SELECT * FROM user_address WHERE user_id = #{userId} ORDER BY is_default DESC, created_at DESC")
    List<UserAddress> selectByUserId(@Param("userId") Integer userId);

    /**
     * 根据用户ID获取默认地址
     */
    @Select("SELECT * FROM user_address WHERE user_id = #{userId} AND is_default = 1 LIMIT 1")
    UserAddress selectDefaultByUserId(@Param("userId") Integer userId);

    /**
     * 取消用户的所有默认地址
     */
    @Update("UPDATE user_address SET is_default = 0 WHERE user_id = #{userId}")
    int cancelAllDefaultByUserId(@Param("userId") Integer userId);

    /**
     * 检查用户是否有地址
     */
    @Select("SELECT COUNT(*) FROM user_address WHERE user_id = #{userId}")
    int countByUserId(@Param("userId") Integer userId);

}
